using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce
{
    /// <summary>
    /// 红点系统实现
    /// </summary>
    public partial class RedDotComponent : GameFrameworkComponent
    {
        /// <summary>
        /// 所有的红点ID
        /// </summary>
        private List<int> m_RedId;

        /// <summary>
        /// 红点拆分出来的Key
        /// </summary>
        private List<string> m_RedKey;

        /// <summary>
        /// 用于存储红点和对应的显示(一个红点最多只能对应一个ID)
        /// </summary>
        private Dictionary<string, List<GameObject>> m_RedDots;

        protected override void Awake()
        {
            base.Awake();

            m_RedDots = new Dictionary<string, List<GameObject>>();

            m_RedId = new List<int>();

            m_RedKey = new List<string>();

            //todo 监听红点变化事件 删除或者增加红点
        }

        /// <summary>
        /// 一次添加多个红点
        /// </summary>
        /// <param name="ids"></param>
        public void AddRedId(List<int> ids)
        {
            //todo 自定义存储红点数据方法 
            for (int i = 0; i < ids.Count; i++)
            {
                AddRedId(ids[i]);
            }
        }

        /// <summary>
        /// 注册红点（一个红点只监听一个红点Id）
        /// </summary>
        /// <param name="redDotType"></param>
        /// <param name="gameObject"></param>
        public void RegisterRedDot(string redDotType, GameObject gameObject)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                m_RedDots[redDotType].Add(gameObject);
            }
            else
            {
                List<GameObject> redGameObjects = new List<GameObject>();

                redGameObjects.Add(gameObject);

                m_RedDots.Add(redDotType, redGameObjects);
            }

            gameObject.SetActive(HasRedDotId(redDotType));
        }

        /// <summary>
        /// 注册红点（一个红点对应多个红点Id）
        /// </summary>
        /// <param name="redDotTypes"></param>
        /// <param name="gameObject"></param>
        public void RegisterRedDot(List<string> redDotTypes, GameObject gameObject)
        {
            for (int i = 0; i < redDotTypes.Count; i++)
            {
                RegisterRedDot(redDotTypes[i], gameObject);
            }
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="redDotType"></param>
        /// <param name="gameObject"></param>
        public void UnRegisterRedDot(string redDotType, GameObject gameObject)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                for (int i = 0; i < m_RedDots[redDotType].Count; i++)
                {
                    if (m_RedDots[redDotType].Contains(gameObject))
                    {
                        m_RedDots[redDotType].Remove(gameObject);

                        gameObject.SetActive(false);
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="redDotTypes"></param>
        /// <param name="gameObject"></param>
        public void UnRegisterRedDot(List<string> redDotTypes, GameObject gameObject)
        {
            for (int i = 0; i < redDotTypes.Count; i++)
            {
                UnRegisterRedDot(redDotTypes[i], gameObject);
            }
        }

        /// <summary>
        /// 检查是否存在红点对象
        /// </summary>
        /// <param name="redDotType"></param>
        public void CheckRedDot(string redDotType)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                for (int i = 0; i < m_RedDots[redDotType].Count; i++)
                {
                    m_RedDots[redDotType][i].SetActive(HasRedDotId(redDotType));
                }
            }

//            if (m_RedDots.ContainsKey(redDotType))
//            {
//                for (int i = 0; i < m_RedDots[redDotType].Count; i++)
//                {
//                    m_RedDots[redDotType][i].SetActive(HasRedDotId((int)redDotType));
//                }
//
//                return true;
//            }
//            return false;
        }

//        /// <summary>
//        /// 检查是否存在红点
//        /// </summary>
//        /// <param name="redDotTypes"></param>
//        /// <param name="objs"></param>
//        /// <returns></returns>
//        public void CheckRedDot(List<string> redDotTypes)
//        {
//            for (int i = 0; i < redDotTypes.Count; i++)
//            {
//                CheckRedDot(redDotTypes[i]);
//            }
//
////            for (int i = 0; i < redDotTypes.Count; i++)
////            {
////                //只要有一个需要点亮,就显示;
////                if (CheckRedDot(redDotTypes[i]))
////                {
////                    return true;
////                }
////            }
////            return false;
//        }


        /// <summary>
        /// 刷新红点显示
        /// </summary>
        public void Refresh()
        {
            foreach (KeyValuePair<string, List<GameObject>> pair in m_RedDots)
            {
                bool active = m_RedKey.Contains(pair.Key);
                
                for (int i = 0; i < pair.Value.Count; i++)
                {
                    pair.Value[i].SetActive(active);
                }
            }
        }

        /// <summary>
        /// 是否存在红点Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool HasRedDotId(string id)
        {
            return m_RedKey.Contains(id);
        }

        /// <summary>
        /// 是否存在红点Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool HasRedDotId(List<string> ids)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                if (HasRedDotId(ids[i]))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 添加一个红点Id
        /// </summary>
        /// <param name="id"></param>
        public void AddRedId(int id)
        {
            if (!m_RedId.Contains(id))
            {
                m_RedId.Add(id);
                string[] s = m_RedDic[(RedDotType)id].Split('/');
                for (int i = 0; i < s.Length; i++)
                {
                    m_RedKey.Add(s[i]);
                    CheckRedDot(s[i]);
                }
                
            }
        }

        /// <summary>
        /// 删掉一个红点Id
        /// </summary>
        /// <param name="id"></param>
        public void RemoveRedId(int id)
        {
            if (m_RedId.Contains(id))
            {
                m_RedId.Remove(id);
                string[] s = m_RedDic[(RedDotType)id].Split('/');
                for (int i = 0; i < s.Length; i++)
                {
                    m_RedKey.Remove(s[i]);
                    CheckRedDot(s[i]);
                }

//                CheckRedDot((RedDotType) id);
            }
        }
    }
}